import {Modal} from 'antd'
import {QRCode} from 'react-qrcode-logo'
import api from './api';
interface ScanQRCodePayParams {
  orderNo: string;
  code_url: string;
  callback: ()=>void;
}


 const TRADE_STATE = {
  SUCCESS: 'SUCCESS',
  NOTPAY: 'NOTPAY'
};
const MAX_CHECK_COUNT = 30;
export function scanQRCodePay({ orderNo, code_url, callback }: ScanQRCodePayParams){
    let interval=0
    const cancelModal = ()=>{
      clearInterval(interval);
      modal.destroy()
      callback?.()
    }
    const modal = Modal.info({
      title:"请支付",
      content:<QRCode value={code_url}/>,
      width:280,
      okText:"取消",
      onOk:()=> cancelModal()
    })
   
    /** 轮训 */
    let checkCount = 0
    interval = setInterval(()=>{
      if(++checkCount > MAX_CHECK_COUNT){
        return cancelModal()
      }
      api.get(`/payment/order/${orderNo}`).then(res=>{
        const { trade_state } = res.data;
        if (trade_state !== TRADE_STATE.NOTPAY) {
          return cancelModal();
        }
      }).catch(()=>{
        cancelModal();
      })
    },1000)
}


